<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'mateName.add'">新增</el-button>
            </div>
            <div class="right-panel">
                <div class="search-input">物料名称</div>
                <el-input v-model="search.keyword" placeholder="请输入物料名称" clearable @change="handleKeyDown"/>
                <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" stripe remoteSort remoteFilter border>
                <el-table-column label="物料类别" prop="cate_name" align="center"></el-table-column>
                <el-table-column label="物料名称" prop="material_name" align="center"></el-table-column>
                <el-table-column label="损耗" prop="loss_ratio" align="center"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                <el-table-column label="排序" prop="sort" align="center" sortable></el-table-column>
                <!-- <el-table-column label="是否禁用" prop="system_disable" align="center">
                    <template #default="scope">
                        <el-switch v-model="scope.row.system_disable" @change="changeSwitchDefault($event, scope.row)" :loading="scope.row.$switch_status" :active-value="1" :inactive-value="0"></el-switch>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button-group>
                            <el-button icon="el-icon-EditPen" text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'mateName.edit'">修改</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button icon="el-icon-Delete" text type="danger" size="small" v-auth="'mateName.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: 'user',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			groupFilterText: '',
			group: [],
			apiObj: this.$API.material.getMaterialList.lists,
			selection: [],
			search: {
				keyword: null,
			},
			//所需数据选项
			cates: [],
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
            console.log('row :>> ', row);
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        goLocation(){
            this.$router.push({
                path: 'storeroomLocation',
            })
        },
		//状态
		async status(row) {
			var reqData = {
				id: row.id,
				is_show :row.is_show
			}
			var res = await this.$API.material.getMaterialList.status.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$message.success("修改成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.material.getMaterialList.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
		},
        // 切换物料状态
        async changeSwitchDefault(val, row) {
			row.$switch_status = true;
			var res = await this.$API.material.getMaterialList.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
        handleKeyDown(){
            this.upsearch()
        },
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
</style>
